package com.heima.comment.service.impl;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.heima.aliyun.AliyunGreenTemplate;

import com.heima.appuser.UserFeignClient;
import com.heima.comment.dtos.CommentSaveDto;
import com.heima.comment.model.entity.ApComment;
import com.heima.comment.service.CommentService;
import com.heima.comment.util.AppThreadLocalUtil;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.entity.ApUser;
import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private UserFeignClient userFeignClient;

    @Resource
    private AliyunGreenTemplate aliyunGreenTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseResult saveComment(CommentSaveDto dto) {
        //1.检查参数
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getContent() != null && dto.getContent().length() > 140) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "评论内容不能超过140字");
        }

        //2.判断是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //3.查询用户信息----- 先从Redis中查询, Redis中不存在再从用户服务查询
        String user_json = stringRedisTemplate.opsForValue().get("LOGIN_USER_" + user.getId());

        if (StringUtils.isEmpty(user_json)) {
            ResponseResult<ApUser> result = userFeignClient.findUserById(user.getId());
            if (result.getCode() == 200 && result.getData() != null) {
                user = result.getData();
                user_json = JSON.toJSONString(user);
                stringRedisTemplate.opsForValue().set("LOGIN_USER_" + user.getId(), user_json, 2, TimeUnit.HOURS);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "当前登录信息有误");
            }
        } else {
            user = JSON.parseObject(user_json, ApUser.class);
        }

        //4.安全过滤,自行实现
        Map<String, String> result = aliyunGreenTemplate.greenTextScan(dto.getContent());
        if (MapUtil.isEmpty(result)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "评论信息违规");
        }

        ApComment apComment = new ApComment();
        apComment.setId(ObjectId.get().toHexString());
        apComment.setUserId(user.getId());
        apComment.setNickName(user.getNicknme());
        apComment.setContent(dto.getContent());
        apComment.setTargetId(dto.getArticleId()+"");
        apComment.setCreatedTime(new Date());
        apComment.setUpdatedTime(new Date());
        apComment.setImage(user.getImage());
        apComment.setLikes(0);
        apComment.setReply(0);

        mongoTemplate.insert(apComment);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}